import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.zs.grain.R

class CommonDialog(context: Context?, themeResId: Int) : Dialog(context, themeResId) {

    /**
     * 建造器的构造方法：
     *
     * @param context
     */

    class Builder (private val context: Context) {
        private var title: String? = null
        private var title2: String? = null
        private var title3: String? = null
        private var title4: String? = null
        private var title5: String? = null
        private var message: String? = null
        private var positiveButtonContent: String? = null
        private var negativeButtonContent: String? = null
        private var otherButtonContent: String? = null
        private var otherButton4Content: String? = null
        private var otherButton5Content: String? = null
        private var otherButton6Content: String? = null
        private var otherButton7Content: String? = null
        private var positiveButtonListener: DialogInterface.OnClickListener? = null
        private var negativeButtonListener: DialogInterface.OnClickListener? = null
        private var otherButtonListener: DialogInterface.OnClickListener? = null
        private var otherButton4Listener: DialogInterface.OnClickListener? = null
        private var otherButton5Listener: DialogInterface.OnClickListener? = null
        private var otherButton6Listener: DialogInterface.OnClickListener? = null
        private var otherButton7Listener: DialogInterface.OnClickListener? = null
        private var contentView: View? = null
        private var imageid: Int = 0
        private var color: Int = 0
        private var withOffSize: Float = 0.toFloat()
        private var heightOffSize: Float = 0.toFloat()


        fun setTitle(title: String): Builder {
            this.title = title
            return this
        }

        fun setTitle2(title: String): Builder {
            this.title2 = title
            return this
        }
        fun setTitle3(title: String): Builder {
            this.title3 = title
            return this
        }
        fun setTitle4(title: String): Builder {
            this.title4 = title
            return this
        }
        fun setTitle5(title: String): Builder {
            this.title5 = title
            return this
        }

        fun setTitle(title: Int): Builder {
            this.title = context.getText(title) as String
            return this
        }

        fun setMessage(message: String): Builder {
            this.message = message
            return this
        }

        fun setMessageColor(color: Int): Builder {
            this.color = color
            return this
        }

        fun setImageHeader(Imageid: Int): Builder {

            this.imageid = Imageid
            return this
        }


        fun setPositiveButton(text: String, listener: DialogInterface.OnClickListener): Builder {
            this.positiveButtonContent = text
            this.positiveButtonListener = listener
            return this
        }

        fun setPositiveButton(textId: Int, listener: DialogInterface.OnClickListener): Builder {
            this.positiveButtonContent = context.getText(textId) as String
            this.positiveButtonListener = listener
            return this
        }

        fun setOther4Button(text: String, listener: DialogInterface.OnClickListener): Builder {
            this.otherButton4Content = text
            this.otherButton4Listener = listener
            return this
        }

        fun setOther4Button(textId: Int, listener: DialogInterface.OnClickListener): Builder {
            this.otherButton4Content = context.getText(textId) as String
            this.otherButton4Listener = listener
            return this
        }

        fun setOther5Button(text: String, listener: DialogInterface.OnClickListener): Builder {
            this.otherButton5Content = text
            this.otherButton5Listener = listener
            return this
        }

        fun setOther5Button(textId: Int, listener: DialogInterface.OnClickListener): Builder {
            this.otherButton5Content = context.getText(textId) as String
            this.otherButton5Listener = listener
            return this
        }

        fun setOther6Button(text: String, listener: DialogInterface.OnClickListener): Builder {
            this.otherButton6Content = text
            this.otherButton6Listener = listener
            return this
        }

        fun setOther6Button(textId: Int, listener: DialogInterface.OnClickListener): Builder {
            this.otherButton6Content = context.getText(textId) as String
            this.otherButton6Listener = listener
            return this
        }

        fun setOther7Button(text: String, listener: DialogInterface.OnClickListener): Builder {
            this.otherButton7Content = text
            this.otherButton7Listener = listener
            return this
        }

        fun setOther7Button(textId: Int, listener: DialogInterface.OnClickListener): Builder {
            this.otherButton7Content = context.getText(textId) as String
            this.otherButton7Listener = listener
            return this
        }


        fun setOtherButton(text: String, listener: DialogInterface.OnClickListener): Builder {
            this.otherButtonContent = text
            this.otherButtonListener = listener
            return this
        }

        fun setOtherButton(textId: Int, listener: DialogInterface.OnClickListener): Builder {
            this.otherButtonContent = context.getText(textId) as String
            this.otherButtonListener = listener
            return this
        }

        fun setNegativeButton(text: String, listener: DialogInterface.OnClickListener): Builder {
            this.negativeButtonContent = text
            this.negativeButtonListener = listener
            return this
        }

        fun setNegativeButton(textId: Int, listener: DialogInterface.OnClickListener): Builder {
            this.negativeButtonContent = context.getText(textId) as String
            this.negativeButtonListener = listener
            return this
        }

        fun setContentView(v: View): Builder {
            this.contentView = v
            return this
        }

        fun setWith(v: Float): Builder {
            this.withOffSize = v
            return this
        }

        fun setContentView(v: Float): Builder {
            this.heightOffSize = v
            return this
        }

        //button1:yew button2:no button3:other choice
        fun create(isTouchCancel:Boolean,dialogLayoutView2:Int,button1:Int,button2:Int=-1,button3:Int=-1,button4:Int=-1,button5:Int=-1,button6:Int=-1,button7:Int=-1): CommonDialog {
            /**
             * 利用我们刚才自定义的样式初始化Dialog
             */
            val dialog = CommonDialog(context,
                    R.style.Dialog)
            /**
             * 下面就初始化Dialog的布局页面
             */
            val inflater = context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val dialogLayoutView = inflater.inflate(dialogLayoutView2,
                    null)
            dialog.addContentView(dialogLayoutView, ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT))

//            if (imageid != 0) {
//                (dialogLayoutView.findViewById<View>(R.id.iv_image_header) as ImageView)
//                        .setImageResource(imageid)
//            } else {
//                (dialogLayoutView.findViewById<View>(R.id.iv_image_header) as ImageView).visibility = View.GONE
//            }

            if (!TextUtils.isEmpty(title)) {
                (dialogLayoutView.findViewById<View>(R.id.text1) as TextView).text = title
            } else {
                // Log.w(context.getClass().toString(), "未设置对话框标题！");
            }

            if (!TextUtils.isEmpty(title2)) {
                (dialogLayoutView.findViewById<View>(R.id.text2) as TextView).text = title2
            }

            if (!TextUtils.isEmpty(title3)) {
                (dialogLayoutView.findViewById<View>(R.id.text3) as TextView).text = title3
            }

            if (!TextUtils.isEmpty(title4)) {
                (dialogLayoutView.findViewById<View>(R.id.text4) as TextView).text = title4
            }

            if (!TextUtils.isEmpty(title5)) {
                (dialogLayoutView.findViewById<View>(R.id.text5) as TextView).text = title5
            }

//            if (color != 0) {//内容
//                val viewById = dialogLayoutView.findViewById<View>(R.id.dialog_content) as TextView
//                viewById.setTextColor(color)
//            }

//            if (!TextUtils.isEmpty(message)) {
//                (dialogLayoutView.findViewById<View>(R.id.dialog_content) as TextView).text = message
//            } else if (contentView != null) {
//                (dialogLayoutView
//                        .findViewById<View>(R.id.dialog_llyout_content) as LinearLayout)
//                        .removeAllViews()
//                (dialogLayoutView
//                        .findViewById<View>(R.id.dialog_llyout_content) as LinearLayout).addView(
//                        contentView, ViewGroup.LayoutParams(
//                        ViewGroup.LayoutParams.WRAP_CONTENT,
//                        ViewGroup.LayoutParams.WRAP_CONTENT))
//            } else {
//                (dialogLayoutView.findViewById<View>(R.id.dialog_content) as TextView).visibility = View.INVISIBLE
//            }

            if (!TextUtils.isEmpty(positiveButtonContent)) {
                (dialogLayoutView.findViewById<View>(button1) as TextView).text = positiveButtonContent
                if (positiveButtonListener != null) {
                    (dialog.findViewById<View>(button1) as TextView)
                            .setOnClickListener { positiveButtonListener!!.onClick(dialog, -1) }

                }
            } else {
                (dialogLayoutView.findViewById<View>(button1) as TextView).visibility = View.GONE
                //dialogLayoutView.findViewById<View>(R.id.line).visibility = View.GONE
            }

            if (!TextUtils.isEmpty(negativeButtonContent)) {
                (dialogLayoutView.findViewById<View>(button2) as TextView).text = negativeButtonContent
                if (negativeButtonListener != null) {
                    (dialogLayoutView
                            .findViewById<View>(button2) as TextView)
                            .setOnClickListener { negativeButtonListener!!.onClick(dialog, -2) }
                }
            } else {
                if(button2!=-1) (dialogLayoutView.findViewById<View>(button2) as TextView).visibility = View.GONE
            }

            if (!TextUtils.isEmpty(otherButtonContent)) {
                (dialogLayoutView.findViewById<View>(button3) as TextView).text = otherButtonContent
                if (otherButtonListener != null) {
                    (dialogLayoutView
                            .findViewById<View>(button3) as TextView)
                            .setOnClickListener { otherButtonListener!!.onClick(dialog, -3) }
                }
            } else {
                if(button3!=-1) (dialogLayoutView.findViewById<View>(button3) as TextView).visibility = View.GONE
            }

            if (!TextUtils.isEmpty(otherButton4Content)) {
                (dialogLayoutView.findViewById<View>(button4) as TextView).text = otherButton4Content
                if (otherButton4Listener != null) {
                    (dialogLayoutView
                            .findViewById<View>(button4) as TextView)
                            .setOnClickListener { otherButton4Listener!!.onClick(dialog, -3) }
                }
            } else {
                if(button4!=-1) (dialogLayoutView.findViewById<View>(button4) as TextView).visibility = View.GONE
            }

            if (!TextUtils.isEmpty(otherButton5Content)) {
                (dialogLayoutView.findViewById<View>(button5) as TextView).text = otherButton5Content
                if (otherButton5Listener != null) {
                    (dialogLayoutView
                            .findViewById<View>(button5) as TextView)
                            .setOnClickListener { otherButton5Listener!!.onClick(dialog, -3) }
                }
            } else {
                if(button5!=-1) (dialogLayoutView.findViewById<View>(button5) as TextView).visibility = View.GONE
            }

            if (!TextUtils.isEmpty(otherButton6Content)) {
                (dialogLayoutView.findViewById<View>(button6) as TextView).text = otherButton6Content
                if (otherButton6Listener != null) {
                    (dialogLayoutView
                            .findViewById<View>(button6) as TextView)
                            .setOnClickListener { otherButton6Listener!!.onClick(dialog, -3) }
                }
            } else {
                if(button6!=-1) (dialogLayoutView.findViewById<View>(button6) as TextView).visibility = View.GONE
            }

            if (!TextUtils.isEmpty(otherButton7Content)) {
                (dialogLayoutView.findViewById<View>(button7) as TextView).text = otherButton7Content
                if (otherButton7Listener != null) {
                    (dialogLayoutView
                            .findViewById<View>(button7) as TextView)
                            .setOnClickListener { otherButton7Listener!!.onClick(dialog, -3) }
                }
            } else {
                if(button7!=-1) (dialogLayoutView.findViewById<View>(button7) as TextView).visibility = View.GONE
            }
            /**
             * 将初始化完整的布局添加到dialog中
             */
            dialog.setContentView(dialogLayoutView)
            /**
             * 禁止点击Dialog以外的区域时Dialog消失
             */
            dialog.setCanceledOnTouchOutside(isTouchCancel)


            val window = dialog.window
            val context = this.context as Activity
            val windowManager = context.windowManager

            val defaultDisplay = windowManager.defaultDisplay

            val attributes = window!!.attributes

            if (withOffSize.toDouble() != 0.0) {

                attributes.width = (defaultDisplay.width * withOffSize).toInt()
            } else {
                attributes.width = (defaultDisplay.width * 0.77).toInt()

            }
            if (heightOffSize.toDouble() != 0.0) {

                attributes.height = (defaultDisplay.height * heightOffSize).toInt()
            }
            window.attributes = attributes
            return dialog
        }
    }

}